package com.longge;

import java.util.HashMap;

/**
 * @program: my-code
 * @description:
 * @author: longge
 * @create: 2021-04-14 01:59
 */
public class CopyRandomList {

    public Node copyRandomList(Node head) {
        HashMap<Node, Node> nextMap = new HashMap<>();
        Node ne = head, res = head;
        while (ne != null) {
            nextMap.put(ne, new Node(ne.val));
            ne = ne.next;
        }

        while (res != null) {
            Node node = nextMap.get(res);
            node.next = nextMap.get(res.next);
            node.random = nextMap.get(res.random);
            res = res.next;
        }
        return nextMap.get(head);
    }


}
